using System;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "Audio/Audio Database")]
public class AudioDataBaseSO : ScriptableObject
{
    public List<AudioClipData> playerAudio;
    public List<AudioClipData> uiAudio;

    public List<AudioClipData> musicList;
    private Dictionary<string, AudioClipData> clipCollection;

    private void OnEnable()
    {
        clipCollection = new Dictionary<string, AudioClipData>();
        AddToCollection(playerAudio);
        AddToCollection(uiAudio);
        AddToCollection(musicList);
    }
    public AudioClipData Get(string groupName) => clipCollection.TryGetValue(groupName, out var data) ? data : null;

    private void AddToCollection(List<AudioClipData> listToAdd)
    {
        foreach (var data in listToAdd)
        {
            if (data != null && clipCollection.ContainsKey(data.audioName) == false)
                clipCollection.Add(data.audioName, data);
        }
    }
}

[Serializable]
public class AudioClipData
{
    public string audioName;
    public List<AudioClip> clips = new();
    [Range(0, 1)] public float maxVolume = 1f;
    public AudioClip GetAudioClip()
    {
        if (clips == null || clips.Count == 0)
            return null;

        return clips[UnityEngine.Random.Range(0, clips.Count)];
    }

}
